<template>
  <div class="bottom-bar">
    <CheckButton class="select-all" @click.native="selectAll" :value="isSelectAll"/>
    <span>全选</span>
    <span class="total-price">合计: ¥{{totalPrice}}</span>
    <span class="buy-product">去计算({{$store.getters.cartListLength}})</span>

  </div>
</template>

<script>
import CheckButton from './CheckButton'
export default {
  name: 'CartBottomBar',
  components: {
    CheckButton
  },
  methods: {
    selectAll() {
      if(this.isSelectAll){
        /* foreach/map */
        return this.$store.state.cartList.forEach(item => 
          item.checked = false)
      }else{
        return this.$store.state.cartList.forEach(item =>
        item.checked = true)
      }
    }
  },
  computed: {
    totalPrice() {
      /* filter筛选 reduce累加 */
      return this.$store.state.cartList.filter((item) => 
        item.checked).reduce(function (preValue,item) {
          return preValue + item.count * item.newPrice
        },0)
    },
    isSelectAll() {
      return this.$store.state.cartList.every(item => 
        item.checked)
    }    
    /**
     * 对数组中每一项运行以下函数，如果都返回true，every返回true
     * 如果有一项返回false，则停止遍历 every返回false
     * 不写默认返回false
     * every()他的返回值是一个boolean类型值。而参数是一个回调函数。
     **/
  }
}
</script>

<style scoped>
.bottom-bar{
  width: 100%;
  height: 50px;
  background-color: rgb(228, 226, 226);
  position: fixed;
  bottom: 50px;
  font-size: 18px;
  color: #000;
  line-height: 50px;
  padding-left: 35px;
  box-sizing: border-box;
}
.select-all {
  line-height: 0;
  position: absolute;
  left: 12px;
  top: 15px;
  }
.total-price {
  margin-left: 40px;
  font-size: 18px;
}

.buy-product {
  background-color: orangered;
  color: #fff;
  width: 100px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  float: right;
}
</style>